#include "Bubble.h"


namespace LZESB
{
Bubble::Bubble()
:m_vertexCount(3)
,m_data(0)
,m_vboId(0)
,m_radius(1.f)
,m_posX(0)
,m_posY(0)
{

}

Bubble::~Bubble()
{
    free(m_data);
    m_data = 0;
}

void Bubble::init(unsigned int  vertexCount,  float x, float y, float radius)
{
    m_vertexCount = vertexCount > 3? vertexCount : 3 ;//must greater than 3
    m_posX = x;
    m_posY = y;
    m_radius = radius;

    m_data = (BubbleVertex*)malloc(m_vertexCount*sizeof(BubbleVertex) );
    for(unsigned int i = 0; i < m_vertexCount; i++)
    {
        m_data[i].x = m_radius * cosf(i * 2 * 3.1415926f /(float)m_vertexCount);
        m_data[i].y = m_radius * sinf(i * 2 * 3.1415926f /(float)m_vertexCount);
        std::cout<<"m_data: "<<m_data->x<<","<<m_data->y<<std::endl;
    }
    glGenBuffers(1, &m_vboId);

}

void Bubble::draw()
{
    glPushMatrix();

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glTranslatef(m_posX, m_posY, 0.f);

   glBindBuffer(GL_ARRAY_BUFFER, m_vboId);
   glBufferData(GL_ARRAY_BUFFER, m_vertexCount*sizeof(BubbleVertex), m_data, GL_STREAM_DRAW);
   glVertexPointer(2, GL_FLOAT, 0, 0);
   glEnableClientState(GL_VERTEX_ARRAY);
   glDrawArrays(GL_TRIANGLE_FAN, 0, m_vertexCount);
   glDisableClientState(GL_VERTEX_ARRAY);

   glPopMatrix();

}



}

